package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall;

import android.content.Context;

import com.diudiutechnology.www.yadiudiu.util.http.BaseBusiness;
import com.diudiutechnology.www.yadiudiu.util.http.BaseResponseBean;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.OkHttpClientManager;
import com.diudiutechnology.www.yadiudiu.util.http.UserCallback;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CallOrderBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CallSingleBannerBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleanProductBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleanTypeBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CreditDetailBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CreditTutorialBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.UnLockingBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.util.HashMap;
import java.util.List;

/**
 * Created by admin on 2017/9/26.
 * 叫单业务处理类
 */

public class CleanBusiness extends BaseBusiness {
    /**
     * 请求叫单的店铺列表
     */
    public static void postBuyACarFragmentModel(String lna, String lng, String pageIndex, String api, Callback.CommonCallback<String> callback) {
        RequestParams params = new RequestParams(api);
        params.addBodyParameter("longitude", lna);
        params.addBodyParameter("latitude", lng);
        params.addBodyParameter("p", pageIndex);
        HttpClient.addPOSTPRICODE(params, callback);
    }

    /**
     * 解析叫单的店铺列表
     *
     * @param json
     * @return
     */
    public static List<CallOrderBean> parseToBuyACarFragmentModel(String json) {
        List<CallOrderBean> bean = null;
        if (json != null) {
            try {
                BaseResponseBean<List<CallOrderBean>> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<List<CallOrderBean>>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }

    /**
     * 开锁店铺的请求
     */
    public static void getBuyACarFragmentModel(Context context, String api, String lng, String lat, UserCallback userCallback) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("longitude", lng);
        map.put("latitude", lat);
        OkHttpClientManager.getInstance(context).getString(api, map, userCallback);
    }

    /**
     * 解析店铺详情
     */
    public static UnLockingBean parseToUnLockingBean(String json) {
        UnLockingBean bean = null;
        if (json != null) {
            try {
                BaseResponseBean<UnLockingBean> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<UnLockingBean>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }

    /**
     * 呼叫开锁
     */
    public static void postLock(Context context, String userid, String longitude, String latitude, String address, String housenumber, String contacts, String phone, UserCallback userCallback) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("userid", userid);
        map.put("longitude", longitude);
        map.put("latitude", latitude);
        map.put("address", address);
        map.put("housenumber", housenumber);
        map.put("contacts", contacts);
        map.put("phone", phone);
        OkHttpClientManager.getInstance(context).postUser(Constant.UNLOCKING_CALLOUT, map, userCallback);
    }

    /**
     * 获取叫单banner
     */
    public static void getCallBanner(Context context, UserCallback userCallback) {
        HashMap<String, String> map = new HashMap<String, String>();
        OkHttpClientManager.getInstance(context).postUser(Constant.BANNER_CALLEDSINGLE, map, userCallback);
    }

    /**
     * 解析叫单banner
     */
    public static CallSingleBannerBean parseToCallSingleBannerBean(String json) {
        CallSingleBannerBean bean = null;
        if (json != null) {
            try {
                BaseResponseBean<CallSingleBannerBean> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<CallSingleBannerBean>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }

    /**
     * 获取干洗分类的数据
     */
    public static void postCleanTypeBean(String api, Callback.CommonCallback<String> callback) {
        RequestParams params = new RequestParams(api);
        HttpClient.addPOSTPRICODE(params, callback);
    }

    /**
     * 解析分类
     */
    public static List<CleanTypeBean> parseToCleanTypeBean(String json) {
        List<CleanTypeBean> bean = null;
        if (json != null) {
            try {
                BaseResponseBean<List<CleanTypeBean>> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<List<CleanTypeBean>>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }

    /**
     * 获取干洗类别商品的数据
     */
    public static void postCleanTypeProductBean(String api, Callback.CommonCallback<String> callback) {
        RequestParams params = new RequestParams(api);
        HttpClient.addPOSTPRICODE(params, callback);
    }

    /**
     * 解析干洗类别商品的数据
     */
    public static List<CleanProductBean> parseToCleanTypeProductBean(String json) {
        List<CleanProductBean> bean = null;
        if (json != null) {
            try {
                BaseResponseBean<List<CleanProductBean>> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<List<CleanProductBean>>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }

    /**
     * 信贷教程
     */
    public static void getCredit(String pageIndex, Callback.CommonCallback<String> callback) {
        RequestParams params = new RequestParams(Constant.CREDIT_INDEX);
        params.addBodyParameter("p", pageIndex);
        HttpClient.addPOSTPRICODE(params, callback);
    }

    /**
     * 解析信贷教程
     */
    public static List<CreditTutorialBean> parseToCreditBean(String json) {
        List<CreditTutorialBean> bean = null;
        if (json != null) {
            try {
                BaseResponseBean<List<CreditTutorialBean>> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<List<CreditTutorialBean>>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }

    /**
     * 信贷详情
     */
    public static void getCreditDetail(Callback.CommonCallback<String> callback) {
        RequestParams params = new RequestParams(Constant.GREDIT_DETAILS);
        HttpClient.addPOSTPRICODE(params, callback);
    }

    /**
     * 解析信贷详情
     */
    public static List<CreditDetailBean> parseToCreditDetailBean(String json) {
        List<CreditDetailBean> bean = null;
        if (json != null) {
            try {
                BaseResponseBean<List<CreditDetailBean>> base = new Gson()
                        .fromJson(
                                json,
                                new TypeToken<BaseResponseBean<List<CreditDetailBean>>>() {
                                }.getType());
                bean = base.result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bean;
        } else {
            return null;
        }
    }
}
